package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.health.pojo.dto.GetByDateDTO;
import com.xbongbong.pro.health.pojo.dto.LineChartDTO;
import com.xbongbong.pro.health.pojo.vo.GetByDateVO;
import com.xbongbong.pro.health.pojo.vo.LineChartVO;
import com.xbongbong.saas.service.HealthService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 系统指标
 * @author chy
 * @version v1.0
 * @date 2019/2/25 17:27
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/health")
public class HealthController {

    @Resource
    private HealthService healthService;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    /**
     * 获取某天的系统指标
     * @param getByDateDTO
     * @param br
     * @return
     * 创建时间 2019/2/25 3:03 PM
     * 修改时间 2019/2/25 3:03 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/getByDate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getByDate(@RequestBody @Valid GetByDateDTO getByDateDTO, BindingResult br) {
        XbbResponse<GetByDateVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GetByDateVO getByDateVO = healthService.getByDate(getByDateDTO);
                response = new XbbResponse<>(getByDateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 获取日期1到日期2的指数折线图
     * @param lineChartDTO
     * @param br
     * @return
     * 创建时间 2019/2/26 3:03 PM
     * 修改时间 2019/2/26 3:03 PM
     * @author chy
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/lineChart", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String lineChart(@RequestBody @Valid LineChartDTO lineChartDTO, BindingResult br) {
        XbbResponse<LineChartVO> response;
        if(br.hasErrors()){
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LineChartVO lineChartVO = healthService.lineChart(lineChartDTO);
                response = new XbbResponse<>(lineChartVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
